<?php

class ShopCardLog extends logCAr
{
    public $order_no;
    public $code_str;
    public $status;
    public $type;
    public $channel_id;



	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/* reset db */
	public function getDbConnection()
	{
	  self::$db=Yii::app()->db1;
	  if(self::$db instanceof CDbConnection) return self::$db;
	}

    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('order_no,code_str,status,type,channel_id,sell_status', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.

        );
    }


    public function tableName()
	{
		return '{{shop_card_log}}';
	}

	public function primaryKey()
	{
		return 'id';
	}


    public static function getAllStock(){
        return Yii::app()->db1->createCommand("select count(*) as stock_num,face_value as face_value from shop_card_log where  sell_status=0 and channel_id=4 group by `face_value`  order by face_value asc ")->queryAll();
    }


	public static function getStock($id){
        $row=Yii::app()->db1->createCommand("select count(*) as cn from shop_card_log where log_id=$id ")->queryRow();
        return $row["cn"];
    }

    public static function getJhStock($id){
        $row=Yii::app()->db1->createCommand("select count(*) as cn from shop_card_log where log_id=$id and status=1 ")->queryRow();
        return $row["cn"];
    }

    public static function getStockByFaceValue($faceValue){
        $row=Yii::app()->db1->createCommand("select count(*) as cn from shop_card_log where face_value=$faceValue and sell_status=0 ")->queryRow();
        return $row["cn"];
    }

	public static function excel($data,$date) {




        // 模拟获取数据

        $obj = new PHPExcel();

        // 列标
        $list = array('A', 'B', 'C');
        // 填充第一行数据
        $obj->getActiveSheet()
            ->setCellValue($list[0] . '1', '激活码')
            ->setCellValue($list[2] . '1', '面值');

        // 填充第n(n>=2, n∈N*)行数据
        $length = count($data);
        for ($i = 0; $i < $length; $i++) {
            $obj->getActiveSheet()->setCellValue($list[0] . ($i + 2), $data[$i]["code_str"], \PHPExcel_Cell_DataType::TYPE_STRING);//将其设置为文本格式
            $obj->getActiveSheet()->setCellValue($list[2] . ($i + 2), $data[$i]["face_value"]);
        }

        // 导出
        ob_clean();
       /* header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename='.$date.'.xlsx');
        header('Cache-Control: max-age=1');

        $objWriter =  PHPExcel_IOFactory::createWriter($obj, 'Excel2007');*/


        $write = new PHPExcel_Writer_Excel5($obj);

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename="'.$date.'.xls"');
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');







      //  $objWriter->save('php://output');
    }

	public function search()
	{




		$criteria=new CDbCriteria;
        $criteria->order =  " id desc ";
        $criteria->compare('order_no',$this->order_no);
        $criteria->compare('status',$this->status);
        $criteria->compare('type',$this->type);


        $aes = new Aes();

        if ($this->code_str) {
            $criteria->compare('code_str', $aes->encrypt($this->code_str));
        }
        $criteria->compare("channel_id",$this->channel_id);
        $criteria->compare("del_flag",0);
        return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}

}
